<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="形状类的设计在上一节中，需要创建Line对象存放线，程序还需要存放Point, Polygon对象，因为Point, Polygon和Line继承于Shape类，便于类的接口统一。  视频链接ShapeClass    1. 类的继承关系点、线、面是同一类，具有相似的行为，如：Draw, Perimeter, Area等，其类的继承关系为图为：   12类的关系图描述了Point, Line, P">
<meta property="og:type" content="article">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="http://example.com/2023/06/05/docs/MapDraw/D4_ShapeClass/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="形状类的设计在上一节中，需要创建Line对象存放线，程序还需要存放Point, Polygon对象，因为Point, Polygon和Line继承于Shape类，便于类的接口统一。  视频链接ShapeClass    1. 类的继承关系点、线、面是同一类，具有相似的行为，如：Draw, Perimeter, Area等，其类的继承关系为图为：   12类的关系图描述了Point, Line, P">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://i.loli.net/2021/07/11/KWPdqH1VabMj59n.png">
<meta property="article:published_time" content="2023-06-05T15:25:53.666Z">
<meta property="article:modified_time" content="2022-02-22T04:10:24.201Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/07/11/KWPdqH1VabMj59n.png">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-docs/MapDraw/D4_ShapeClass" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2023/06/05/docs/MapDraw/D4_ShapeClass/" class="article-date">
  <time class="dt-published" datetime="2023-06-05T15:25:53.666Z" itemprop="datePublished">2023-06-05</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h1 id="形状类的设计"><a href="#形状类的设计" class="headerlink" title="形状类的设计"></a>形状类的设计</h1><p>在上一节中，需要创建Line对象存放线，程序还需要存放Point, Polygon对象，因为Point, Polygon和Line继承于Shape类，便于类的接口统一。</p>
<ul>
<li>视频链接<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Sy4y1K7k7?share_source=copy_web">ShapeClass</a><iframe src="//player.bilibili.com/player.html?aid=804069056&bvid=BV1Sy4y1K7k7&cid=367800938&page=1" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"> </iframe></li>
</ul>
<hr>
<h2 id="1-类的继承关系"><a href="#1-类的继承关系" class="headerlink" title="1. 类的继承关系"></a>1. 类的继承关系</h2><p>点、线、面是同一类，具有相似的行为，如：Draw, Perimeter, Area等，其类的继承关系为图为：<br><img src="https://i.loli.net/2021/07/11/KWPdqH1VabMj59n.png" alt="Shape_class"></p>
<!--![Shape_class](../../png/Shape_class.png)-->

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">类的关系图描述了Point, Line, Polygon 均从Shape基类继承而成。同时Line和Polygon对象中还包含Point对象，描述的是组合关系。</span><br><span class="line">考虑到Draw函数的差异性，将Draw函数抽象为Shape的虚函数，事实上，可以抽象为纯虚函数，这样Shape就变成了抽象类。</span><br></pre></td></tr></table></figure>

<h2 id="2-类的接口设计"><a href="#2-类的接口设计" class="headerlink" title="2. 类的接口设计"></a>2. 类的接口设计</h2><p>分析程序的需求，需要Shape类提供什么接口呢？有无统一的接口呢？如有统一的接口，则需要添加为基类的虚函数。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">记住：程序的设计阶段无需考虑编码的实现，此处专注的是功能接口的参数定义、返回值等。</span><br></pre></td></tr></table></figure>

<p>可能的接口包括：</p>
<ul>
<li>Draw - 绘制图形</li>
<li>Area - 计算面积</li>
<li>Perimeter - 周长</li>
<li>read - 从文件读</li>
<li>write - 写入文件</li>
<li>Move - 移动坐标</li>
<li>…</li>
</ul>
<h2 id="2-1-Shape类"><a href="#2-1-Shape类" class="headerlink" title="2.1 Shape类"></a>2.1 Shape类</h2><p>Shape类设计为抽象类，具有纯虚函数，这样的目的是为了统一接口，统一接口的好处在随后的实现中会体现出来，大家也可以在代码实现后好好阅读和体会。</p>
<ul>
<li>Shape.h<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;graphics.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> MapDraw &#123;	<span class="comment">//名字空间，解决类的同名冲突</span></span><br><span class="line">	<span class="keyword">class</span> <span class="title class_">Shape</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">friend</span> ostream&amp; <span class="keyword">operator</span>&lt;&lt;(ostream&amp; os, <span class="type">const</span> Shape&amp; shp);</span><br><span class="line">		<span class="keyword">friend</span> istream&amp; <span class="keyword">operator</span>&gt;&gt;(istream&amp; is, Shape&amp; shp);</span><br><span class="line">	<span class="keyword">public</span>:</span><br><span class="line">		<span class="built_in">Shape</span>();</span><br><span class="line">		<span class="keyword">virtual</span> ~<span class="built_in">Shape</span>() = <span class="keyword">default</span>;</span><br><span class="line"></span><br><span class="line">		<span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">Draw</span><span class="params">()</span> </span>= <span class="number">0</span>;				</span><br><span class="line">		<span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">Move</span><span class="params">(<span class="type">int</span> dx, <span class="type">int</span> dy)</span> </span>= <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">protected</span>:</span><br><span class="line">		<span class="function"><span class="keyword">virtual</span> <span class="type">bool</span> <span class="title">write</span><span class="params">(ostream&amp; os)</span> <span class="type">const</span> </span>= <span class="number">0</span>;	<span class="comment">//纯虚函数</span></span><br><span class="line">		<span class="function"><span class="keyword">virtual</span> <span class="type">bool</span> <span class="title">read</span><span class="params">(istream&amp; is)</span> </span>= <span class="number">0</span>;			<span class="comment">//纯虚函数</span></span><br><span class="line"></span><br><span class="line">		COLORREF	m_penClr;	<span class="comment">//画笔颜色</span></span><br><span class="line">		COLORREF	m_fillClr;	<span class="comment">//填充颜色</span></span><br><span class="line">		<span class="type">int</span>			m_nSize;	<span class="comment">//大小</span></span><br><span class="line">	&#125;;</span><br><span class="line"></span><br><span class="line">	ostream&amp; <span class="keyword">operator</span>&lt;&lt;(ostream&amp; os, <span class="type">const</span> Shape&amp; shp);</span><br><span class="line">	istream&amp; <span class="keyword">operator</span>&gt;&gt;(istream&amp; is, Shape&amp; shp);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>Shape.cpp</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">namespace</span> MapDraw &#123;</span><br><span class="line">	Shape::<span class="built_in">Shape</span>()</span><br><span class="line">	&#123;</span><br><span class="line">		m_penClr = RED;	<span class="comment">//画笔颜色</span></span><br><span class="line">		m_fillClr = GREEN;	<span class="comment">//填充颜色</span></span><br><span class="line">		m_nSize = <span class="number">1</span>;	<span class="comment">//大小</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 注意此处的operator&lt;&lt; 和operator&gt;&gt;重载函数，调用的是write和read虚函数，是为了实现多态。</span></span><br><span class="line">	ostream&amp; <span class="keyword">operator</span>&lt;&lt;(ostream&amp; os, <span class="type">const</span> Shape&amp; shp)</span><br><span class="line">	&#123;</span><br><span class="line">		shp.<span class="built_in">write</span>(os);</span><br><span class="line">		<span class="keyword">return</span> os;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	istream&amp; <span class="keyword">operator</span>&gt;&gt;(istream&amp; is, Shape&amp; shp)</span><br><span class="line">	&#123;</span><br><span class="line">		shp.<span class="built_in">read</span>(is);</span><br><span class="line">		<span class="keyword">return</span> is;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="2-2-Point类"><a href="#2-2-Point类" class="headerlink" title="2.2 Point类"></a>2.2 Point类</h3><p>Point类实现了write 和 read 函数，其目的是从输入流读入x，y坐标，将x,y写出到输出流。点文件的格式为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">0 10 10</span><br><span class="line">点类型 x y</span><br></pre></td></tr></table></figure>
<ul>
<li>Point.h<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">namespace</span> MapDraw &#123;</span><br><span class="line">	<span class="keyword">class</span> <span class="title class_">Point</span> : <span class="keyword">public</span> Shape</span><br><span class="line">	&#123;</span><br><span class="line">	<span class="keyword">public</span>:</span><br><span class="line">		<span class="built_in">Point</span>(<span class="type">int</span> x = <span class="number">0</span>, <span class="type">int</span> y = <span class="number">0</span>);</span><br><span class="line">		~<span class="built_in">Point</span>() = <span class="keyword">default</span>;</span><br><span class="line"></span><br><span class="line">		<span class="function"><span class="type">void</span> <span class="title">Draw</span><span class="params">()</span> <span class="keyword">override</span></span>;</span><br><span class="line">		<span class="function"><span class="type">void</span> <span class="title">Move</span><span class="params">(<span class="type">int</span> dx, <span class="type">int</span> dy)</span> <span class="keyword">override</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">protected</span>:</span><br><span class="line">		<span class="function"><span class="type">bool</span> <span class="title">write</span><span class="params">(ostream&amp; os)</span> <span class="type">const</span> <span class="keyword">override</span></span>;</span><br><span class="line">		<span class="function"><span class="type">bool</span> <span class="title">read</span><span class="params">(istream&amp; is)</span> <span class="keyword">override</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">protected</span>:</span><br><span class="line">		<span class="type">int</span>			m_x, m_y;</span><br><span class="line">	&#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>Point.cpp<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;Point.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> MapDraw &#123;</span><br><span class="line">	Point::<span class="built_in">Point</span>(<span class="type">int</span> x, <span class="type">int</span> y) : <span class="built_in">Shape</span>(), <span class="built_in">m_x</span>(x), <span class="built_in">m_y</span>(y)</span><br><span class="line">	&#123;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">Point::Draw</span><span class="params">()</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="built_in">putpixel</span>(m_x, m_y, RED);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">Point::Move</span><span class="params">(<span class="type">int</span> dx, <span class="type">int</span> dy)</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		m_x += dx;</span><br><span class="line">		m_y += dy;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="type">bool</span> <span class="title">Point::write</span><span class="params">(ostream&amp; os)</span> <span class="type">const</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		os &lt;&lt; <span class="number">0</span> &lt;&lt; <span class="string">&quot;\t&quot;</span> &lt;&lt; m_x &lt;&lt; <span class="string">&quot;\t&quot;</span> &lt;&lt; m_y &lt;&lt; endl;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//此处没有读入点类型0，为何？？</span></span><br><span class="line">	<span class="function"><span class="type">bool</span> <span class="title">Point::read</span><span class="params">(istream&amp; is)</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		is &gt;&gt; m_x &gt;&gt; m_y;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="2-3-Line类"><a href="#2-3-Line类" class="headerlink" title="2.3 Line类"></a>2.3 Line类</h3><p>与点的类类似，Line类存储线的坐标，其数据存储样式为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1	3	10	0	20	20	50	5</span><br><span class="line">点类型 节点数 x1 y1 x2 y2 x3 y3</span><br></pre></td></tr></table></figure>
<p>此处重点关注，Line类的write()和read()。</p>
<ul>
<li><p>Line.h</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;Shape.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;Line.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;initializer_list&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> MapDraw &#123;</span><br><span class="line">	<span class="keyword">class</span> <span class="title class_">Line</span> : <span class="keyword">public</span> Shape</span><br><span class="line">	&#123;</span><br><span class="line">	<span class="keyword">public</span>:</span><br><span class="line">		<span class="built_in">Line</span>(POINT* <span class="type">const</span> pts = <span class="literal">nullptr</span>, <span class="type">int</span> n = <span class="number">0</span>);</span><br><span class="line">		<span class="built_in">Line</span>(<span class="type">const</span> vector&lt;POINT&gt;&amp; pts);</span><br><span class="line">		<span class="built_in">Line</span>(<span class="type">const</span> initializer_list&lt;POINT&gt;&amp; pts);</span><br><span class="line">		~<span class="built_in">Line</span>();</span><br><span class="line"></span><br><span class="line">		<span class="function"><span class="type">void</span> <span class="title">Draw</span><span class="params">()</span> <span class="keyword">override</span></span>;</span><br><span class="line">		<span class="function"><span class="type">void</span> <span class="title">Move</span><span class="params">(<span class="type">int</span> dx, <span class="type">int</span> dy)</span> <span class="keyword">override</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">protected</span>:</span><br><span class="line">		<span class="function"><span class="type">bool</span> <span class="title">write</span><span class="params">(ostream&amp; os)</span> <span class="type">const</span> <span class="keyword">override</span></span>;</span><br><span class="line">		<span class="function"><span class="type">bool</span> <span class="title">read</span><span class="params">(istream&amp; is)</span> <span class="keyword">override</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">protected</span>:</span><br><span class="line">		vector&lt;POINT&gt;	m_nodes;</span><br><span class="line">	&#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>Line.cpp</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;Line.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> MapDraw &#123;</span><br><span class="line">	Line::<span class="built_in">Line</span>(POINT* <span class="type">const</span> pts, <span class="type">int</span> n)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; ++i)</span><br><span class="line">		&#123;</span><br><span class="line">			m_nodes.<span class="built_in">push_back</span>(pts[i]);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	Line::<span class="built_in">Line</span>(<span class="type">const</span> vector&lt;POINT&gt;&amp; pts) : <span class="built_in">m_nodes</span>(pts)</span><br><span class="line">	&#123;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	Line::<span class="built_in">Line</span>(<span class="type">const</span> initializer_list&lt;POINT&gt;&amp; pts) : <span class="built_in">m_nodes</span>(pts)</span><br><span class="line">	&#123;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	Line::~<span class="built_in">Line</span>()</span><br><span class="line">	&#123;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">Line::Draw</span><span class="params">()</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="built_in">polyline</span>(m_nodes.<span class="built_in">data</span>(), m_nodes.<span class="built_in">size</span>());</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">Line::Move</span><span class="params">(<span class="type">int</span> dx, <span class="type">int</span> dy)</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="type">bool</span> <span class="title">Line::write</span><span class="params">(ostream&amp; os)</span> <span class="type">const</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		os &lt;&lt; <span class="number">1</span> &lt;&lt; <span class="string">&quot;\t&quot;</span> &lt;&lt; m_nodes.<span class="built_in">size</span>() &lt;&lt; <span class="string">&quot;\t&quot;</span>;</span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">auto</span>&amp; pt : m_nodes)</span><br><span class="line">			os &lt;&lt; pt.x &lt;&lt; <span class="string">&quot;\t&quot;</span> &lt;&lt; pt.y &lt;&lt; <span class="string">&quot;\t&quot;</span>;</span><br><span class="line">		os &lt;&lt; endl;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="type">bool</span> <span class="title">Line::read</span><span class="params">(istream&amp; is)</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="type">size_t</span>	ncount;</span><br><span class="line">		POINT	pt;</span><br><span class="line">		is &gt;&gt; ncount;</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; ncount; ++i)</span><br><span class="line">		&#123;</span><br><span class="line">			is &gt;&gt; pt.x &gt;&gt; pt.y;</span><br><span class="line">			m_nodes.<span class="built_in">push_back</span>(pt);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="2-4-Polygon类"><a href="#2-4-Polygon类" class="headerlink" title="2.4 Polygon类"></a>2.4 Polygon类</h3><p>Polygon类与Line类差别不大，区别是要首尾封闭。为了简化代码，Polygon类从Line类继承，在Draw()和write()函数略有不同，注意观察二者之间的细微差别。</p>
<ul>
<li>Polygon.h<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;Line.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> MapDraw &#123;</span><br><span class="line">	<span class="keyword">class</span> <span class="title class_">Polygon</span> : <span class="keyword">public</span> Line</span><br><span class="line">	&#123;</span><br><span class="line">	<span class="keyword">public</span>:</span><br><span class="line">		<span class="built_in">Polygon</span>(POINT* <span class="type">const</span> pts = <span class="literal">nullptr</span>, <span class="type">int</span> n = <span class="number">0</span>);</span><br><span class="line">		<span class="built_in">Polygon</span>(<span class="type">const</span> vector&lt;POINT&gt;&amp; pts);</span><br><span class="line">		<span class="built_in">Polygon</span>(<span class="type">const</span> initializer_list&lt;POINT&gt;&amp; pts);</span><br><span class="line">		~<span class="built_in">Polygon</span>() = <span class="keyword">default</span>;</span><br><span class="line"></span><br><span class="line">		<span class="function"><span class="type">void</span> <span class="title">Draw</span><span class="params">()</span> <span class="keyword">override</span></span>;</span><br><span class="line">	<span class="keyword">protected</span>:</span><br><span class="line">		<span class="function"><span class="type">bool</span> <span class="title">write</span><span class="params">(ostream&amp; os)</span> <span class="type">const</span> <span class="keyword">override</span></span>;</span><br><span class="line">	&#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>Polygon.cpp<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;Polygon.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> MapDraw &#123;</span><br><span class="line">	Polygon::<span class="built_in">Polygon</span>(POINT* <span class="type">const</span> pts, <span class="type">int</span> n) : <span class="built_in">Line</span>(pts, n)	&#123;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	Polygon::<span class="built_in">Polygon</span>(<span class="type">const</span> vector&lt;POINT&gt;&amp; pts) : <span class="built_in">Line</span>(pts)</span><br><span class="line">	&#123;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	Polygon::<span class="built_in">Polygon</span>(<span class="type">const</span> initializer_list&lt;POINT&gt;&amp; pts) : <span class="built_in">Line</span>(pts)</span><br><span class="line">	&#123;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">Polygon::Draw</span><span class="params">()</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="built_in">polygon</span>(m_nodes.<span class="built_in">data</span>(), m_nodes.<span class="built_in">size</span>());</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="type">bool</span> <span class="title">Polygon::write</span><span class="params">(ostream&amp; os)</span> <span class="type">const</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		os &lt;&lt; <span class="number">2</span> &lt;&lt; <span class="string">&quot;\t&quot;</span> &lt;&lt; m_nodes.<span class="built_in">size</span>() &lt;&lt; <span class="string">&quot;\t&quot;</span>;</span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">auto</span>&amp; pt : m_nodes)</span><br><span class="line">			os &lt;&lt; pt.x &lt;&lt; <span class="string">&quot;\t&quot;</span> &lt;&lt; pt.y &lt;&lt; <span class="string">&quot;\t&quot;</span>;</span><br><span class="line">		os &lt;&lt; endl;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="3-小结"><a href="#3-小结" class="headerlink" title="3. 小结"></a>3. 小结</h2><p>通过此节的内容，可实现点、线、多边形的存取。体会设计虚函数的目。只有单个的shape对象，还不足以存储程序绘制的多个形状，如何存储多个形状呢，下一节介绍ShapeSet类，用于组织多个形状序列。</p>
<p>Go to | <a href="../README.md">Home</a> | <a href="#%E5%BD%A2%E7%8A%B6%E7%B1%BB%E7%9A%84%E8%AE%BE%E8%AE%A1">Head</a> | <a href="./D5_ShapeSets.md">ShapeSets-&gt;Next</a> | <a href="./D3_DrawMsg.md">DrawMsg&lt;-Prev</a> |</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2023/06/05/docs/MapDraw/D4_ShapeClass/" data-id="clij09mc30005c8n57g0acsgp" data-title="" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2023/06/05/docs/MapDraw/D5_ShapeSets/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="/2023/06/05/docs/MapDraw/D1_MainFrame/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title"></div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/06/">June 2023</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/06/05/png/RSCmdManual/">(no title)</a>
          </li>
        
          <li>
            <a href="/2023/06/05/docs/RSImage/Subject/">(no title)</a>
          </li>
        
          <li>
            <a href="/2023/06/05/docs/RSImage/Statistics/">(no title)</a>
          </li>
        
          <li>
            <a href="/2023/06/05/docs/RSImage/RSImageFormat/">(no title)</a>
          </li>
        
          <li>
            <a href="/2023/06/05/docs/RSImage/RSCmdManual/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>